-- IS_INVALID_DUNGEON_GRID

-- 是否无效地牢格子
-- @param       class   格子类别
-- @param       element 格子
return function(class, element)
    local args = DungeonM.queryElement(element, "args") or {};
    local gridType = DungeonM.query(class, "type");

    local dbase = DungeonM.query(class, "dbase") or {};

    -- 如果是GRID_TYPE_FEATURE_ITEM
    if gridType == GRID_TYPE_FEATURE_ITEM then
        local rule = FeatureItemM.query(class, "rule");
        -- 是否已经拾取过
        if FeatureItemM.isFeatureOpen(rule) then
            -- 只能出现一次，所以失效了
            return true;
        end

        -- 如果是哈文的魔宠，那么要确认下是否开启
        if dbase["is_petcamp_card"] == 1 and not MonthCardM.isCurTypeCardOpen("pet_camp_card") then
            return true;
        end
    end

    -- 如果是药剂配方的载体
    if dbase["formula_appear"] ~= nil then
        local formulaId = DungeonM.query(class, "dbase")["formula_appear"];
        local reagentId = ReagentM.getReagentByFormula(formulaId);

        -- 已有配方或者已经开放对应试剂，则失效
        if ItemM.getAmount(ME.user, formulaId) > 0 or
            ReagentM.isReagentActive(reagentId) then
            return true;
        end
    end

    -- 如果是恶魔雕像，需要判断是否都已经满级了
    if gridType == GRID_TYPE_DEMON_STATUE then
        local ret = true;
        for _, class in pairs(DemonStatueM.getAllStatue()) do
            if not DemonStatueM.isTopLevel(class) then
                ret = false;
                break;
            end
        end

        return ret;
    end

    -- 如果是邪神雕像，需要判断是否合法
    if gridType == GRID_TYPE_EVIL_STATUE then
        local dbase = DungeonM.queryElement(element, "dbase");
        return EvilStatueM.isInvalid(dbase);
    end

    -- 如果不是”沙漠绿洲“中出现的沙漠祭坛
    local dungeonId = DungeonM.getDungeonId();
    if gridType == GRID_TYPE_DESERT_ALTAR and DungeonAreaM.getParentId(dungeonId) ~= 8 then
        -- 需要判断黄金灯油是否足够
        local cost = DesertAltarM.calcSubmitCost(2);
        for classId, amount in pairs(cost) do
            if ItemM.getAmount(ME.user, classId) < amount then
                return true;
            end
        end
    end

    -- 如果不是”德古拉城堡“中出现的鮮血祭坛
    local dungeonId = DungeonM.getDungeonId();
    local parentId = DungeonAreaM.getParentId(dungeonId);
    local layer = DungeonM.currentLayer();
    if gridType == GRID_TYPE_BLOOD_ALTAR and parentId ~= 10 and parentId ~= 30001 or
        gridType == GRID_TYPE_BLOOD_ALTAR and parentId == 30001 and layer > 1 then
        -- 需要判断是否能升级
        local classId = 8124;
        local base_cost = HolyItemsM.calcUpgradeBaseLevelCost(classId);
        local extra_cost = HolyItemsM.calcUpgradeExtraLevelCost(classId);
        local cur_base_level = HolyItemsM.getBaseLevel(classId);
        local cur_extra_level = HolyItemsM.getExtraLevel(classId);
        local require_base_level = FormulaM.invoke("CALC_HOLY_ITEMS_BASE_LEVEL_REQUIRE", classId, cur_extra_level);
        local max_base_level = HolyItemsM.query(classId, "max_base_level");
        local max_extra_level = HolyItemsM.query(classId, "max_extra_level");

        -- 基本属性、特殊属性都满级
        if cur_base_level == max_base_level and
            cur_extra_level == max_extra_level then
            return true;
        end
        -- 基本属性满级、特殊属性未满，但先祖之血不够
        if cur_base_level == max_base_level and
            ItemM.getAmount(ME.user, extra_cost[2]) < extra_cost[3] then
            return true;
        end
        -- 特殊属性满级、基本属性未满，但精血不够
        if cur_extra_level == max_extra_level and
            ItemM.getAmount(ME.user, base_cost[2]) < base_cost[3] then
            return true;
        end
        -- 精血、先祖之血都不够
        if ItemM.getAmount(ME.user, base_cost[2]) < base_cost[3] and
            ItemM.getAmount(ME.user, extra_cost[2]) < extra_cost[3] then
            return true;
        end
        -- 精血不够、先祖之血足够但基础等级不够升级
        if ItemM.getAmount(ME.user, base_cost[2]) < base_cost[3] and
            cur_base_level < require_base_level then
            return true;
        end
    end

    -- 如果还没有开启拼图功能，拼图奖励为无效的格子
    local puzzleClass = {374, 375};

    if gridType == GRID_TYPE_ITEM and table.indexOf(puzzleClass, class) ~= -1 then
        local puzzleBonus = args.bonus or {};
        -- 如果没有开放，那么当前奖励无效
        if DungeonPuzzleM.isInValidPuzzle(puzzleBonus) then
            return true;
        end
    end

    -- 如果还没有开启勋章,那么勋章勋章奖励不能直接奖励(哈文实验室的骨头勋章)
    if gridType == GRID_TYPE_ITEM and dbase["is_sky_medal"] == 1 then
        -- 如果勋章还没开放，那么非法
        if not SkyRelicFunctionM.isServerSkyMedalOpen() then
            return true;
        end
    end

    return false;
end
